package com.blog.controller;

import com.blog.domain.ResponseResult;
import com.blog.domain.entity.LoginUser;
import com.blog.domain.entity.Menu;
import com.blog.domain.entity.User;
import com.blog.domain.vo.AdminUserInfoVo;
import com.blog.domain.vo.RoutersVo;
import com.blog.domain.vo.UserInfoVo;
import com.blog.enums.AppHttpCodeEnum;
import com.blog.exception.SystemException;
import com.blog.service.LoginService;
import com.blog.service.MenuService;
import com.blog.service.RoleService;
import com.blog.utils.BeanCopyUtils;
import com.blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class LoginController {

	@Autowired
	private LoginService loginService;

	@Autowired
	private MenuService menuService;

	@Autowired
	private RoleService roleService;



	@PostMapping("/user/login")
	public ResponseResult login(@RequestBody User user){

		//必须传用户名

		if (!StringUtils.hasText(user.getUserName())){
			throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
		}

		return loginService.login(user);

	}


	@GetMapping("/getInfo")
	public ResponseResult<AdminUserInfoVo> getInfo(){

		//获取当前登录的用户
		LoginUser loginUser = SecurityUtils.getLoginUser();

		//根据用户id查询权限信息
		Long userid = loginUser.getUser().getId();
		List<String> perm = menuService.selectPermByUserId(userid);

		//获取用户信息,userInfo 类型
		User user = loginUser.getUser();
		UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);

		//根据用户id查询角色信息
		List<String> rolekey = roleService.selectRoleKeyByUserId(userid);
		//封装返回
		AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perm,rolekey,userInfoVo);

		return ResponseResult.okResult(adminUserInfoVo);

	}


	@GetMapping("getRouters")
	public ResponseResult<RoutersVo> getRouters(){
		Long userId = SecurityUtils.getUserId();
		//查询menu 结果是tree的形式
		List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);
		//封装数据返回
		return ResponseResult.okResult(new RoutersVo(menus));
	}


	/**退出登录
	 *
	 * @return
	 */
	@PostMapping("/user/logout")
	public ResponseResult logout(){

		return loginService.logout();


	}
}
